﻿using System;
using System.Data;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Web.UI.WebControls;

namespace VIT.Web.Admin
{
    public partial class SupplierList : VITComponent
    {
        Provider.ProductSupplier obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new Provider.ProductSupplier();

            if (!Page.IsPostBack)
            {
            }            
        }

        #region Methods
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable GetData(string SortColumn)
        {
            Provider.ProductSupplier l = new Provider.ProductSupplier();
            return l.GetAll(SortColumn);
        }

        public void deleteList(string[] listId)
        {
            foreach (string id in listId)
                obj.Delete(1, int.Parse(id));
        }

        public void PublishList(int pub, string[] listId)
        {
            foreach (string id in listId)
                obj.Publish(pub, int.Parse(id));
        }
        #endregion

        #region
        protected void Delete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            deleteList(check);
            ListView.DataBind();
        }

        protected void Insert_Click(object sender, EventArgs e)
        {
            RedirectComponent("Product", "SupplierEdit");
        }

        protected void Published_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(1, check);
            ListView.DataBind();
        }

        protected void UnPublished_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(0, check);
            ListView.DataBind();
        }
        
        protected void changePublish_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            int pub = (id[1] == "True") ? 0 : 1;
            obj.Publish(pub, int.Parse(id[0]));
            ListView.DataBind();
        }

        protected void updateOrder_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            obj.Order(id[1], int.Parse(id[0]));
            ListView.DataBind();
        }

        protected void Pager_PreRender(object sender, EventArgs e)
        {
            ListView.DataBind();
        }
        #endregion
    }
}
